using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Globalization;
using System.Threading;
using System.Data;
using System.Security.Cryptography;
namespace Test {
	class FileCatalog {
		static DAL dal=new DAL();
		static void MainX() {

			Thread.CurrentThread.CurrentCulture=new CultureInfo(1053,false);
			dal.NonQry("truncate table FileCache");
			List<string> folders=new List<string>();
			//foreach(string str in new Settings("fca").GetValue("src") as string[])
			//  if(!str.StartsWith("-"))
			//    folders.Add(str);

			//folders.AddRange(Mixer.CombineEx(@"\\pon-wpct{0:00}\c$\Program Files\Warehouse Systems\Next.Whs.FaultyReturns","51-75"));
			//folders.Add(@"\\pon-wprf01-cl01\c$\Program Files\Next.Whs.DirectoryPicking.NewRF");
			//folders.Add(@"\\pon-wprf01-cl02\c$\Program Files\Next.Whs.DirectoryPicking.NewRF");
			//folders.Add(@"C:\RBuffTest\p1\Next.Whs.ReturnsBuffer.Proxy");
			//folders.Add(@"C:\RBuffTest\p2\Next.Whs.ReturnsBuffer.Proxy");
			//folders.Add(@"\\end-epdf01\ECommerce\Software");
			//folders.Add(@"T:\Common\BSY\xp\");

			folders.Add(@"C:\RetTest\3.0.2.8 Live\src");
			folders.Add(@"C:\RetTest\3.0.2.8 FromMsi\");
		//folders.Add(@"\\pon-wprf01-cl01\c$\Program Files\RF");
			//folders.Add(@"\\pon-wprf01-cl02\c$\Program Files\RF");

			foreach(string folder in folders)
			{
				System.Diagnostics.Stopwatch sw=System.Diagnostics.Stopwatch.StartNew();
				//dal.NonQry("create table FileCache(id int identity,dir varchar(1024),name varchar(255),ext varchar(10),cT datetime,lwT datetime,laT datetime,len bigint)");
				List<string> files=SearchFilesRecursive(folder,"*.*");
				Console.WriteLine(sw.Elapsed);
			}
		}
		public static List<string> SearchFilesRecursive(string directory,string pattern) {
			Queue<string> stack=new Queue<string>();
			List<string> result=new List<string>();
			stack.Enqueue(directory);
			//List<string> current_dir=new List<string>();
			int extention_length=pattern.Length-1;
			while(stack.Count>0)
			{
				string dir=stack.Dequeue();
				try
				{
					string[] files=Directory.GetFiles(dir,pattern);
					//current_dir.AddRange();
					foreach(string file in files)
					{
						FileInfo inf=new FileInfo(file);
						string hash="";

						//-------------------calc hash---------------------
						using(MD5 md5=MD5.Create())
						using(Stream stream=File.OpenRead(file))
							hash=BitConverter.ToString(md5.ComputeHash(stream)).Replace("-","").ToLower();
						//-------------------calc hash---------------------
						//if(inf.CreationTime>DateTime.Now.AddDays(-2))
						//  result.Add(file);
						//if(inf.CreationTime>DateTime.Now.AddDays(-30))
						dal.NonQry("insert into FileCache(dir,name,ext,cT,lwT,laT,len,hash)values('{0}','{1}','{2}','{3}','{4}','{5}',{6},'{7}')",inf.DirectoryName.Replace("'","''"),inf.Name.Replace("'","''"),inf.Extension.Replace("'","''"),inf.CreationTime,inf.LastWriteTime,inf.LastAccessTime,inf.Length,hash);
					}
					//--------------sub-dirs----------------------
					foreach(string dn in Directory.GetDirectories(dir))
						stack.Enqueue(dn);
				}
				catch(Exception ex)
				{
					Console.WriteLine(ex.Message+":"+dal.lastSql);
					// Couldn't open the directory
				}
			}
			return result;
		}
	}
}
